package com.lbyl.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.lbyl.activitiy.R;

/**
 * 自定义的下拉刷新listciew.附加了下拉刷新的动画小窗
 *
 * @author libin996@outlook.com
 */

public class PullToRefreshListView extends ListView implements AbsListView.OnScrollListener {

    private static final String TAG = "PullToRefreshListView";
    private static int SCROLLSTATE = 0;
    public static int STATE_CURRENT = 0;
    public static final int STATE_PULL_TO_REFRESH = 1;//滑动显示出刷新块
    public static final int STATE_RELEASE_TO_REFRESH = 2;//滑动到指定位置
    public static final int STATE_REFRESHING = 3;//正在刷新

    private OnRefreshListener mOnRefreshListener;

    private View headerView;
    private TextView textview_header_status_topnews;
    private ImageView imageview_header_arrow_topnews;
    private ProgressBar progressBar_header_topnews;
    private View footerView;
    private TextView textview_footer_status_topnews;
    private ImageView imageview_footer_arrow_topnews;
    private ProgressBar progressBa_footer_topnews;
    private float startY;
    private int measuredHeight_header;


    public PullToRefreshListView(Context context) {
        super(context);
        initHeaderView();
        initFootView();
    }


    public PullToRefreshListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initHeaderView();
        initFootView();

    }

    public PullToRefreshListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initHeaderView();
        initFootView();
    }

    /**
     * 设置监听接口的回调
     *
     * @param listener
     */
    public void setOnRefreshListener(OnRefreshListener listener) {
        this.mOnRefreshListener = listener;
    }


    /**
     * 初始化刷新块."正在刷新"+转圈
     */
    public void initHeaderView() {
        //导入头布局控件
        headerView = View.inflate(getContext(), R.layout.pull_to_refresh_header, new LinearLayout(getContext()));
        headerView.setVisibility(GONE);
        //镶嵌刷新块
        this.addHeaderView(headerView, null, false);
        textview_header_status_topnews = (TextView) headerView.findViewById(R.id.textview_header_status_topnews);
        imageview_header_arrow_topnews = (ImageView) findViewById(R.id.imageview_header_arrow_topnews);
        progressBar_header_topnews = (ProgressBar) headerView.findViewById(R.id.progressBar_header_topnews);
        //隐藏刷新块
        headerView.measure(0, 0);//0是view中常量,代表不指定大小
        measuredHeight_header = headerView.getMeasuredHeight();
        headerView.setPadding(0, -measuredHeight_header, 0, 0);
    }

    /**
     * 初始化脚布局
     */
    private void initFootView() {
        footerView = View.inflate(getContext(), R.layout.pull_to_refresh_footer, null);
        textview_footer_status_topnews = (TextView) footerView.findViewById(R.id.textview_header_status_topnews);
        imageview_footer_arrow_topnews = (ImageView) footerView.findViewById(R.id.imageview_header_arrow_topnews);
        progressBa_footer_topnews = (ProgressBar) footerView.findViewById(R.id.progressBar_header_topnews);
        footerView.measure(0, 0);
        measuredHeight_header = footerView.getMeasuredHeight();
        footerView.setPadding(0, -measuredHeight_header, 0, 0);
        this.addFooterView(footerView);
        //设置监听器
        this.setOnScrollListener(this);
    }


    /**
     * 滑动状态变化,提前加载
     *
     * @param view
     * @param scrollState
     */
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        this.SCROLLSTATE = scrollState;
        if (scrollState != SCROLL_STATE_IDLE) {//必须是非惯性滑动状态
            return;
        }
        //如果是空闲状态
        int position = getLastVisiblePosition();
        if (position == getCount() - 1) {//显示到最后的条目
            footerView.setPadding(0, 0, 0, 0);
            setSelection(position);
            //通知主页面加载下一页
            // TODO: 2016/10/8/0008 上拉加载更多1/.*789
            if (mOnRefreshListener != null) {
                mOnRefreshListener.onLoadMore();
            }
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

    }

    /**
     * 点击改变状态
     *
     * @param ev
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent ev) {


        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startY = ev.getY();
                break;

            case MotionEvent.ACTION_MOVE:


                float endY = ev.getY();
                float dY = endY - startY;

                if (dY > 0 && getFirstVisiblePosition() == 0) {//正在最上方向下滑,应使刷新块显示
                    headerView.setVisibility(View.VISIBLE);

                    int padding = (int) dY - measuredHeight_header;//随手指动态变化的坐标
                    if (padding < 0) {//正在下滑.设置状态为下滑,显示"下拉更新"
                        headerView.setPadding(0, padding, 0, 0);
                        STATE_CURRENT = STATE_PULL_TO_REFRESH;
                        refreshHeaderUIByState();

                    } else if (padding >= 0 && STATE_CURRENT != STATE_RELEASE_TO_REFRESH) {//到达最低端,应提示松开以刷新
                        STATE_CURRENT = STATE_RELEASE_TO_REFRESH;
                        refreshHeaderUIByState();

                    }
                }
                break;
            case MotionEvent.ACTION_UP:
                if (STATE_CURRENT == STATE_RELEASE_TO_REFRESH) {//松开手指,进行刷新
                    STATE_CURRENT = STATE_REFRESHING;
                    headerView.setPadding(0, 0, 0, 0);
                    if (mOnRefreshListener != null) {
                        mOnRefreshListener.onRefreshStart();
                    }
                    refreshHeaderUIByState();

                    //完整显示头布局
                    // TODO: 2016/10/10/0010 刷新过程中的停滞动画 
                    headerView.setPadding(0,-measuredHeight_header,0,0);
                    headerView.setVisibility(GONE);
                } else if (STATE_CURRENT == STATE_PULL_TO_REFRESH) {//没有到达指定位置就松手,取消刷新,隐藏头布局
                    headerView.setPadding(0, -measuredHeight_header, 0, 0);
                    headerView.setVisibility(View.GONE);
                }
                break;
        }

        return super.onTouchEvent(ev);
    }

    /**
     * 更新刷新块UI
     */
    private void refreshHeaderUIByState() {
        switch (STATE_CURRENT) {
            case STATE_PULL_TO_REFRESH:
                textview_header_status_topnews.setText("下拉刷新");
                break;
            case STATE_RELEASE_TO_REFRESH:
                textview_header_status_topnews.setText("释放以刷新");
                break;
            case STATE_REFRESHING:
                textview_header_status_topnews.setText("正在刷新");

                break;
        }
    }

    public interface OnRefreshListener {
        /**
         * 下拉刷新的回调接口
         */
        void onRefreshStart();

        /**
         * 上拉加载更多的接口
         */
        void onLoadMore();

    }

}




